<template>
  <div class="store-list-container">
    <div class="page-header">
      <h2>门店列表</h2>
      <p>管理商户旗下的门店信息和状态</p>
    </div>

    <!-- 搜索栏 -->
    <div class="search-bar">
      <a-row :gutter="16">
        <a-col :span="5">
          <a-input
            v-model:value="searchForm.storeName"
            placeholder="门店名称"
            allow-clear
            @press-enter="handleSearch"
          />
        </a-col>
        <a-col :span="5">
          <a-input
            v-model:value="searchForm.merchantName"
            placeholder="所属商户"
            allow-clear
            @press-enter="handleSearch"
          />
        </a-col>
        <a-col :span="4">
          <a-select
            v-model:value="searchForm.status"
            placeholder="门店状态"
            allow-clear
            style="width: 100%"
          >
            <a-select-option
              v-for="status in statusOptions"
              :key="status.value"
              :value="status.value"
            >
              {{ status.label }}
            </a-select-option>
          </a-select>
        </a-col>
        <a-col :span="4">
          <a-select
            v-model:value="searchForm.storeType"
            placeholder="门店类型"
            allow-clear
            style="width: 100%"
          >
            <a-select-option
              v-for="type in storeTypeOptions"
              :key="type"
              :value="type"
            >
              {{ type }}
            </a-select-option>
          </a-select>
        </a-col>
        <a-col :span="6">
          <a-space>
            <a-button type="primary" @click="handleSearch">
              <template #icon><search-outlined /></template>
              搜索
            </a-button>
            <a-button @click="handleReset">重置</a-button>
            <a-button type="primary" @click="handleAdd">
              <template #icon><plus-outlined /></template>
              新增门店
            </a-button>
          </a-space>
        </a-col>
      </a-row>
    </div>

    <!-- 数据表格 -->
    <div class="table-container">
      <a-table
        :columns="columns"
        :data-source="filteredData"
        :loading="loading"
        :pagination="pagination"
        row-key="id"
        :scroll="{ x: 1200 }"
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'status'">
            <a-tag
              :color="statusOptions.find(s => s.value === record.status)?.color"
            >
              {{ statusOptions.find(s => s.value === record.status)?.label }}
            </a-tag>
          </template>

          <template v-else-if="column.key === 'action'">
            <a-space>
              <a-button
                type="link"
                size="small"
                @click="handleView(record)"
              >
                查看
              </a-button>
              <a-button
                type="link"
                size="small"
                @click="handleEdit(record)"
              >
                编辑
              </a-button>
              <a-button
                v-if="record.status === 'active'"
                type="link"
                size="small"
                danger
                @click="handleDisable(record)"
              >
                禁用
              </a-button>
              <a-button
                v-else
                type="link"
                size="small"
                @click="handleEnable(record)"
              >
                启用
              </a-button>
            </a-space>
          </template>
        </template>
      </a-table>
    </div>

    <!-- 详情弹窗 -->
    <a-modal
      v-model:open="detailVisible"
      title="门店详情"
      :footer="null"
      width="800px"
    >
      <div v-if="selectedRecord" class="detail-content">
        <a-descriptions :column="2" bordered>
          <a-descriptions-item label="门店编号">
            {{ selectedRecord.id }}
          </a-descriptions-item>
          <a-descriptions-item label="门店名称">
            {{ selectedRecord.storeName }}
          </a-descriptions-item>
          <a-descriptions-item label="所属商户">
            {{ selectedRecord.merchantName }}
          </a-descriptions-item>
          <a-descriptions-item label="联系人">
            {{ selectedRecord.contactPerson }}
          </a-descriptions-item>
          <a-descriptions-item label="联系电话">
            {{ selectedRecord.contactPhone }}
          </a-descriptions-item>
          <a-descriptions-item label="门店状态">
            <a-tag
              :color="statusOptions.find(s => s.value === selectedRecord?.status)?.color"
            >
              {{ statusOptions.find(s => s.value === selectedRecord?.status)?.label }}
            </a-tag>
          </a-descriptions-item>
          <a-descriptions-item label="门店地址" :span="2">
            {{ selectedRecord.address }}
          </a-descriptions-item>
          <a-descriptions-item label="营业时间">
            {{ selectedRecord.businessHours }}
          </a-descriptions-item>
          <a-descriptions-item label="创建时间">
            {{ selectedRecord.createTime }}
          </a-descriptions-item>
          <a-descriptions-item label="门店类型">
            {{ selectedRecord.storeType }}
          </a-descriptions-item>
          <a-descriptions-item label="门店面积">
            {{ selectedRecord.area }}㎡
          </a-descriptions-item>
          <a-descriptions-item label="员工数量">
            {{ selectedRecord.employeeCount }}人
          </a-descriptions-item>
          <a-descriptions-item label="月营业额" v-if="selectedRecord.monthlyRevenue">
            ¥{{ selectedRecord.monthlyRevenue?.toLocaleString() }}
          </a-descriptions-item>
          <a-descriptions-item label="门店描述" :span="2" v-if="selectedRecord.description">
            {{ selectedRecord.description }}
          </a-descriptions-item>
        </a-descriptions>
      </div>
    </a-modal>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import {
  SearchOutlined,
  PlusOutlined
} from '@ant-design/icons-vue'
import { storeData, statusOptions, storeTypeOptions, type Store } from './data'

// 响应式数据
const loading = ref(false)
const detailVisible = ref(false)
const selectedRecord = ref<Store | null>(null)

// 搜索表单
const searchForm = reactive({
  storeName: '',
  merchantName: '',
  status: undefined,
  storeType: undefined
})

// 表格列定义
const columns = [
  {
    title: '门店编号',
    dataIndex: 'id',
    key: 'id',
    width: 120,
    fixed: 'left'
  },
  {
    title: '门店名称',
    dataIndex: 'storeName',
    key: 'storeName',
    width: 180
  },
  {
    title: '所属商户',
    dataIndex: 'merchantName',
    key: 'merchantName',
    width: 150
  },
  {
    title: '门店类型',
    dataIndex: 'storeType',
    key: 'storeType',
    width: 100
  },
  {
    title: '联系人',
    dataIndex: 'contactPerson',
    key: 'contactPerson',
    width: 100
  },
  {
    title: '联系电话',
    dataIndex: 'contactPhone',
    key: 'contactPhone',
    width: 120
  },
  {
    title: '门店地址',
    dataIndex: 'address',
    key: 'address',
    width: 200
  },
  {
    title: '营业时间',
    dataIndex: 'businessHours',
    key: 'businessHours',
    width: 120
  },
  {
    title: '状态',
    dataIndex: 'status',
    key: 'status',
    width: 100
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    key: 'createTime',
    width: 150
  },
  {
    title: '操作',
    key: 'action',
    width: 180,
    fixed: 'right'
  }
]

// 分页配置
const pagination = reactive({
  current: 1,
  pageSize: 10,
  total: 0,
  showSizeChanger: true,
  showQuickJumper: true,
  showTotal: (total: number) => `共 ${total} 条记录`
})

// 计算过滤后的数据
const filteredData = computed(() => {
  let data = [...storeData]

  if (searchForm.storeName) {
    data = data.filter(item =>
      item.storeName.toLowerCase().includes(searchForm.storeName.toLowerCase())
    )
  }

  if (searchForm.merchantName) {
    data = data.filter(item =>
      item.merchantName.toLowerCase().includes(searchForm.merchantName.toLowerCase())
    )
  }

  if (searchForm.status) {
    data = data.filter(item => item.status === searchForm.status)
  }

  if (searchForm.storeType) {
    data = data.filter(item => item.storeType === searchForm.storeType)
  }

  pagination.total = data.length
  return data
})

// 搜索
const handleSearch = () => {
  pagination.current = 1
}

// 重置搜索
const handleReset = () => {
  Object.assign(searchForm, {
    storeName: '',
    merchantName: '',
    status: undefined,
    storeType: undefined
  })
  pagination.current = 1
}

// 新增门店
const handleAdd = () => {
  message.info('新增门店功能开发中')
}

// 查看详情
const handleView = (record: Store) => {
  selectedRecord.value = record
  detailVisible.value = true
}

// 编辑门店
const handleEdit = (record: Store) => {
  message.info(`编辑门店: ${record.storeName}`)
}

// 禁用门店
const handleDisable = (record: Store) => {
  message.success(`已禁用门店: ${record.storeName}`)
}

// 启用门店
const handleEnable = (record: Store) => {
  message.success(`已启用门店: ${record.storeName}`)
}

// 组件挂载时加载数据
onMounted(() => {
  // 这里可以从API加载数据
})
</script>

<style scoped>
.store-list-container {
  padding: 24px;
  background: #fff;
  border-radius: 8px;
}

.page-header {
  margin-bottom: 24px;
}

.page-header h2 {
  margin: 0 0 8px 0;
  font-size: 20px;
  font-weight: 600;
}

.page-header p {
  margin: 0;
  color: #666;
}

.search-bar {
  margin-bottom: 24px;
  padding: 20px;
  background: #fafafa;
  border-radius: 6px;
}

.table-container {
  margin-top: 16px;
}

.detail-content {
  max-height: 600px;
  overflow-y: auto;
}
</style>